#tag Window
Begin Window SearchWin
   BackColor       =   16777215
   Backdrop        =   ""
   CloseButton     =   True
   Composite       =   False
   Frame           =   0
   FullScreen      =   False
   HasBackColor    =   False
   Height          =   400
   ImplicitInstance=   True
   LiveResize      =   True
   MacProcID       =   0
   MaxHeight       =   32000
   MaximizeButton  =   False
   MaxWidth        =   32000
   MenuBar         =   ""
   MenuBarVisible  =   True
   MinHeight       =   64
   MinimizeButton  =   False
   MinWidth        =   64
   Placement       =   0
   Resizeable      =   True
   Title           =   "Search"
   Visible         =   True
   Width           =   500
   Begin StaticText searchTermsST
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   0
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Search terms:"
      TextAlign       =   0
      TextColor       =   0
      TextFont        =   "System"
      TextSize        =   11
      TextUnit        =   0
      Top             =   14
      Underline       =   ""
      Visible         =   True
      Width           =   72
   End
   Begin StaticText catalogST
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   1
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Catalog:"
      TextAlign       =   0
      TextColor       =   0
      TextFont        =   "System"
      TextSize        =   11
      TextUnit        =   0
      Top             =   45
      Underline       =   ""
      Visible         =   True
      Width           =   65
   End
   Begin TextField srchkey
      AcceptTabs      =   ""
      Alignment       =   0
      AutoDeactivate  =   True
      BackColor       =   16777215
      Bold            =   ""
      Border          =   True
      CueText         =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Format          =   ""
      Height          =   22
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   97
      LimitText       =   0
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   True
      LockTop         =   True
      Mask            =   ""
      Password        =   ""
      ReadOnly        =   ""
      Scope           =   0
      TabIndex        =   2
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   ""
      TextColor       =   0
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   12
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   383
   End
   Begin PopupMenu catmenu
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   22
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      InitialValue    =   ""
      Italic          =   ""
      Left            =   97
      ListIndex       =   0
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Scope           =   0
      TabIndex        =   0
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   44
      Underline       =   ""
      Visible         =   True
      Width           =   311
   End
   Begin PushButton PushButton1
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "Search"
      Default         =   True
      Enabled         =   True
      Height          =   22
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   420
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   True
      LockTop         =   True
      Scope           =   0
      TabIndex        =   4
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   44
      Underline       =   ""
      Visible         =   True
      Width           =   60
   End
   Begin Listbox searchset
      AutoDeactivate  =   True
      AutoHideScrollbars=   True
      Bold            =   ""
      Border          =   True
      ColumnCount     =   3
      ColumnsResizable=   True
      ColumnWidths    =   "40%,40%,20%"
      DataField       =   ""
      DataSource      =   ""
      DefaultRowHeight=   -1
      Enabled         =   True
      EnableDrag      =   ""
      EnableDragReorder=   ""
      GridLinesHorizontal=   0
      GridLinesVertical=   0
      HasHeading      =   True
      HeadingIndex    =   -1
      Height          =   280
      HelpTag         =   ""
      Hierarchical    =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      InitialValue    =   "			"
      Italic          =   ""
      Left            =   20
      LockBottom      =   True
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   True
      LockTop         =   True
      RequiresSelection=   ""
      Scope           =   0
      ScrollbarHorizontal=   ""
      ScrollBarVertical=   True
      SelectionType   =   0
      TabIndex        =   5
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   100
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   460
      _ScrollWidth    =   -1
   End
   Begin CheckBox extendedCB
      AutoDeactivate  =   True
      Bold            =   ""
      Caption         =   "Extended search"
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   97
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Scope           =   0
      State           =   0
      TabIndex        =   6
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   78
      Underline       =   ""
      Value           =   False
      Visible         =   True
      Width           =   159
   End
   Begin StaticText hakutulos
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   True
      Left            =   201
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   7
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   ""
      TextAlign       =   0
      TextColor       =   0
      TextFont        =   "System"
      TextSize        =   10
      TextUnit        =   0
      Top             =   78
      Underline       =   ""
      Visible         =   True
      Width           =   279
   End
End
#tag EndWindow

#tag WindowCode
	#tag Event
		Sub Open()
		  me.left=SCREEN(0).WIDTH/2-me.width/2
		  me.top=100
		  
		  app.lang.LoadCaptions(self)
		End Sub
	#tag EndEvent


	#tag Method, Flags = &h0
		Function DoSearch(key as string, ext as boolean) As boolean
		  dim rs as recordset
		  dim sql as string
		  
		  if catmenu.ListCount>0 then
		    if catmenu.listindex=-1 then catmenu.listindex=0
		    
		    
		    Searchset.deleteallrows
		    
		    if mydb<>nil then
		      
		      if not ext then
		        
		        sql="SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE trackname LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        sql=sql+" UNION SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE performer LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        sql=sql+" UNION SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE albumname LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        
		        
		      else
		        
		        sql="SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE trackname LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        sql=sql+" UNION SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE performer LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        sql=sql+" UNION SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE albumname LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        sql=sql+" UNION SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE authors LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        sql=sql+" UNION SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE composers LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        sql=sql+" UNION SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE lyricist LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        sql=sql+" UNION SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE arranger LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        sql=sql+" UNION SELECT objectid o,trackname t,performer p,duration d FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE soloist LIKE '%"+key+"%' AND CATEGORYID='"+str(catmenu.RowTag(catmenu.listindex))+"'"
		        
		      end
		      
		      rs=mydb.SQLSelect(sql)
		      
		      if mydb.error then MsgBox mydb.errormessage+EndOfLine+EndOfLine+sql
		      
		      if rs<>nil then
		        
		        hakutulos.text="Hakutulos: "+str(rs.recordcount)+" teosta."
		        
		        while not rs.eof
		          
		          searchset.addrow DefineEncoding(rs.field("t"),Encodings.UTF8)
		          searchset.rowtag(searchset.lastindex)=rs.field("o").IntegerValue
		          searchset.cell(searchset.lastindex,1)=DefineEncoding(rs.field("p"),Encodings.UTF8)
		          searchset.cell(searchset.lastindex,2)=DefineEncoding(rs.field("d"),Encodings.UTF8)
		          
		          rs.movenext
		          
		        wend
		        
		        rs.close
		        
		      end
		    end
		    
		  end
		  
		  return true
		End Function
	#tag EndMethod


#tag EndWindowCode

#tag Events catmenu
	#tag Event
		Sub Open()
		  if mydb<>nil then
		    
		    dim rs as recordset
		    rs=mydb.SQLSelect("SELECT * FROM "+app.mprefs.getstring("dbschema")+".catalog ORDER BY rowid")
		    if rs<>nil then
		      while not rs.eof
		        me.addrow DefineEncoding(rs.field("value"),Encodings.UTF8)
		        me.rowtag(me.listcount-1)=rs.field("rowid").Integervalue
		        rs.movenext
		      wend
		    end
		  end
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events PushButton1
	#tag Event
		Sub Action()
		  do
		  loop until DoSearch(srchkey.text,extendedCB.value)
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events searchset
	#tag Event
		Sub DoubleClick()
		  if me.listindex>-1 then
		    
		    dim c as integer
		    c=me.rowtag(me.listindex)
		    
		    MainWin.TheSong=New MusicDataObject
		    
		    do
		    loop until MainWin.TheSong.GetFromDb(c)
		    
		    self.hide
		    
		  end
		End Sub
	#tag EndEvent
	#tag Event
		Function KeyDown(Key As String) As Boolean
		  if key=CHR(127) then
		    
		    if app.IsAdmin then
		      
		      dim d as new MessageDialog
		      dim b as MessageDialogButton
		      
		      d.icon=MessageDialog.GraphicCaution
		      
		      d.CancelButton.Visible=True    //show the Cancel button
		      d.AlternateActionButton.Visible=False  //show the "Don't Save" button
		      
		      if App.CurrentLang="EN" then
		        d.ActionButton.Caption="Delete"
		        d.CancelButton.Caption="Cancel"
		        d.Message="Do you really want to delete the track?"
		        d.Explanation="You cannot undo this operation. "
		      elseif App.CurrentLang="FI" then
		        d.ActionButton.Caption="Poista"
		        d.CancelButton.Caption="Peruuta"
		        d.Message="Haluatko varmasti poistaa kappaleen?"
		        d.Explanation="Toimintoa ei voi peruuttaa. "
		      end
		      
		      b=d.ShowModal //display the dialog
		      
		      
		      Select Case b //determine which button was pressed.
		        
		      Case d.ActionButton
		        
		        if mydb<>nil then
		          
		          mydb.SQLExecute("DELETE FROM "+app.mprefs.getstring("dbschema")+".musicinfo WHERE objectid="+str(me.rowtag(me.listindex)))
		          
		          if mydb.error then
		            MsgBox mydb.ErrorMessage
		          else
		            do
		            loop until DoSearch(srchkey.text,extendedCB.value)
		          end
		          
		        end
		        
		        
		      Case d.CancelButton
		        
		      End select
		      
		    end
		    
		  end
		End Function
	#tag EndEvent
#tag EndEvents
